//
//  DetailViewController.m
//  WDFans
//
//  Created by Zhihong Yuan on 2/10/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "Detail2ViewController.h"
#import "WDNews.h"
#import "ZHDataService.h"
#import "ActivityViewManager.h"
#import "ForwardWeiboViewController.h"
#import "WDFansAppDelegate.h"

@implementation Detail2ViewController

@synthesize currentNews;
@synthesize myWebView;
@synthesize delegate;

- (IBAction)back:(id)sender
{
    [delegate backClick];
//    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)shareClick:(id)sender
{
    UIActionSheet* as = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"分享到微博",@"通过邮件分享",nil];
    [as showInView:self.view];
    [as release];
}


- (IBAction)collectClick:(id)sender
{
    NSString *path=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
    
    NSString *filename=[path stringByAppendingPathComponent:@"WDNews"]; 
    NSData *fileData=[NSData dataWithContentsOfFile:filename];
    NSMutableArray* array = [NSKeyedUnarchiver unarchiveObjectWithData:fileData];
    
    if (array == nil) {
        array = [NSMutableArray array];
    }
    
    for (NSInteger i=0; i<[array count]; i++) {
        
        WDNews *news = (WDNews *)[array objectAtIndex:i];
        if ([news.newsTitle isEqualToString:self.currentNews.newsTitle]) {
            [array removeObjectAtIndex:i];
            break;
        }
    }
    
    if([array count]>=100){
        [array removeObjectAtIndex:0];
    }
    
    [array addObject:self.currentNews];
    
    NSData *freezeDrid = [NSKeyedArchiver archivedDataWithRootObject:array];
    [freezeDrid writeToFile:filename atomically:YES];
    
    [ActivityViewManager showErrorViewWithMessage:@"已收藏"];
}


#pragma mark - AbstractTabViewController methods
- (IBAction)rightItemClicked
{
    [self back:nil];
    WDFansAppDelegate *appDelegate = (WDFansAppDelegate *)[UIApplication sharedApplication].delegate;
    
    ForwardWeiboViewController *forwardController = [[ForwardWeiboViewController alloc] init];
    forwardController.weiboText = @"我正在试用西部数据粉丝俱乐部iphone版应用.";
    UINavigationController *naviController = [[UINavigationController alloc] initWithRootViewController:forwardController];
    [appDelegate.rootController presentModalViewController:naviController animated:YES];
    [naviController release];
    [forwardController release];
}


#pragma mark - View lifecycle
- (void)dealloc
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [myWebView release];
    [currentNews release];
    [super dealloc];
}




- (id)initWithNews:(WDNews *)curNews
{
//    self = [super initWithNibName:@"DetailViewController" bundle:nil];
    self = [super initWithNibName:@"Detail2ViewController" bundle:nil];
    if (self) {
        // Custom initialization
        self.currentNews = curNews;        
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //    self.title = self.currentNews.newsTitle;
    self.title = @"热点咨询";
    myWebView.backgroundColor = [UIColor clearColor];
//    [self setBackgroundImageViewForTabView];
    [self addNaviBarRightItemWithTitle:@"分享微博"];
    if(SYSTEW_VERSION_LESS_THAN(@"5.0"))
    {
        UIScrollView *scrollView = (UIScrollView *)[[self.myWebView subviews] objectAtIndex:0];
        if(scrollView)
        {
            scrollView.bounces = NO;
        }
    }
    else
        self.myWebView.scrollView.bounces = NO;
    
//    self.currentNews.newsContent = [NSString stringWithFormat:@"<P align=center><font size=5>%@</font></P><P align=center>%@</P>%@",self.currentNews.newsTitle,self.currentNews.newsDate,self.currentNews.newsContent];
    
    [self.myWebView loadHTMLString:[NSString stringWithFormat:@"<font size=4 color=#070707><P align=center>%@</br></font><font size=3 color=gray>%@</font><hr></P><font size=4 color=#4F4F4F>%@</font>",self.currentNews.newsTitle,self.currentNews.newsDate,self.currentNews.newsContent] baseURL:nil];
    self.myWebView.hidden = YES;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.currentNews = nil;
    self.myWebView = nil;
}

#pragma mark - UIWebviewDelegate methods
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    NSString *font = (NSString *)[[NSUserDefaults standardUserDefaults] objectForKey:kFontKey];
    if([font isEqualToString:@"大"])
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'"];
    else if([font isEqualToString:@"中"])
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '90%'"];
    else
        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '80%'"];
    webView.hidden = NO;
}

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
    return navigationType != UIWebViewNavigationTypeLinkClicked;
}


#pragma mark -
#pragma mark UIActionSheetDelegate methods
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if(buttonIndex==1)
    {
        if([MFMailComposeViewController canSendMail] )
        {
            MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
            picker.mailComposeDelegate = self;
            [picker setSubject:nil];
            [picker setToRecipients:nil];
            [picker setCcRecipients:nil];	
            [picker setBccRecipients:nil];
            [picker setMessageBody:nil isHTML:YES];
            [self presentModalViewController:picker animated:YES];
            [picker release];
        }
        else {
            UIAlertView* av = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先配置手机的邮件账户后发送邮件" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [av show];
            [av release];
        }
    }
    else if(buttonIndex==0){
        [self rightItemClicked];
    }
}

#pragma mark - Delegates
#pragma mark MFMailComposeViewControllerDelegate delegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    [controller dismissModalViewControllerAnimated:YES];
}


@end
